Solarian Programmer

My programming ramblings

Clang and Objective-C on Windows

Posted on March 21, 2012 by Paul

UPDATE 17 May 2014

Newer versions of Clang and LLVM seems to have a problem with GNUstep dev tools which are based on the now old gcc-4.6.1 compiler. I’ve changed the post to avoid this problem by using r181679 which works with GNUstep.

UPDATE 13 May 2013

For your convenience, I’ve uploaded on github a binary version of Clang and GNUstep. My recommendation is to compile it yourself as described in the article. If you need a quick way to use Clang and GNUSstep, you can use the linked binary: https://github.com/sol-prog/Clang_GNUstep_Objective-C_for_Windows

If you want to start learning Objective-C on a Windows computer, you’ve come to the right place. This tutorial will show you how to install a compiler and the necessary frameworks to start hacking Objective-C on Windows today. Be warned that I’m not talking about developing the next Objective-C iPhone/iPad application on Windows, this is not possible at the time of this writing!

A prerequisite for a successful install is the Python interpreter. Go to http://www.python.org/ and install Python 2.7.x on your machine. After that you will need to update your path to include C:\Python27.

We’ll start with installing GNUstep, which aims to be “a free and open version of the Cocoa (formerly known as NeXTSTEP/OpenStep) APIs and tools”. In the next step, we will install Clang and I will show you how to write and compile a small Objective-C program.

Navigate to GNUstep Windows, download and install in this order: GNUstep MSYS System, GNUstep Core, GNUSstep Devel. From now on I will assume that you’ve installed GNUstep in C:\GNUstep. If you’ve installed GNUstep on a different location, please replace every occurrence of C:\GNUstep in the next steps accordingly.

GNUstep comes with the GCC compiler which, in principle, can be used to compile Objective-C programs. However in the last years Apple has added some important extensions to Objective-C, that are not available with gcc. Apple’s default compiler is Clang which is able to compile any modern Objective-C code. Since Clang doesn’t have an official Windows installer at the time of this writing we are going to compile it from sources.

Open a GNUstep shell window, you can find this in Start-GNUstep-Shell and write/paste the next lines one by one:

1 svn co -r 181679 http://llvm.org/svn/llvm-project/llvm/trunk llvm
2 cd llvm/tools
3 svn co -r 181679 http://llvm.org/svn/llvm-project/cfe/trunk clang

Now, we need to edit some hard coded paths from clang. Without this, clang won’t be able to find your header files. Navigate to your HOME folder, which on my machine is on C:\GNUstep\msys\1.0\home\sol, in your case this should something like C:\GNUstep\msys\1.0\home\name. Enter in llvm-tools-clang-lib-Frontend and open in your favorite text editor (I recommend Notepad++ or SublimeText 2) InitHeaderSearch.cpp. Search for the line that starts with “FIXME: temporary hack: hard-coded paths.” at about line 215 and add these lines:

 1         ...
 2         // FIXME: temporary hack: hard-coded paths.
 3         AddPath("C:\\GNUstep\\include", System, false);
 4         AddPath("C:\\GNUstep\\msys\\1.0\include", System, false);
 5         AddPath("C:\\GNUstep\\msys\\1.0\include-fixed", System, false);
 6         AddPath("C:\\GNUstep\\lib\\gcc\\mingw32\\4.6.1\\include", System, false);
 7         AddPath("C:\\GNUstep\\lib\\gcc\\mingw32\\4.6.1\\include\\c++", System, false);
 8         AddPath("C:\\GNUstep\\lib\\gcc\\mingw32\\4.6.1\\include\\c++\\mingw32", System, false);
 9         AddPath("C:\\GNUstep\\lib\\gcc\\mingw32\\4.6.1\\include\\c++\\backward", System, false);
10         AddPath("C:\\GNUstep\\GNUstep\\System\\Library\\Headers", System,false);
11         //AddPath("/usr/local/include", System, true, false, false);
12         break;
13         ....

Don’t forget to comment line 11 in the above. Save and close the file. Next, we will create a build directory and we will configure the code for compilation:

1 cd ~/llvm && mkdir build && cd build
2 ../configure --enable-optimized --enable-targets=host --enable-docs=no

Now, we are ready to compile Clang, this will take some time:

1 make && make install

Please note that in the above I’ve explicitly chose to compile the code on a single processor, keep the make line this way.

Let’s check if clang works:

1 clang --version

You should see something like:

1 clang version 3.4 (trunk 181679)
2 Target: i686-pc-mingw32
3 Thread model: posix

Let’s try to compile a small Objective-C code in order to test our new compiler and libraries. Please note that this example won’t work with GCC and GNUstep, it will, however compile with Clang and GNUstep.

Create a new folder in your GNUstep HOME and in this folder two files: GNUmakefile and test.m:

1 cd ~
2 mkdir first_objc && cd first_objc
3 touch GNUmakefile test.m

Open test.m in your text editor, copy and save the next Objective-C code:

1 #include <Foundation/Foundation.h>
2 
3 int main(){
4     @autoreleasepool{
5         NSLog( @"Objective-C has ARC now :D");
6     }
7     return 0;
8 }

Copy and save the next code in GNUmakefile:

1 include $(GNUSTEP_MAKEFILES)/common.make
2 
3 TOOL_NAME = test
4 test_OBJC_FILES = test.m
5 
6 include $(GNUSTEP_MAKEFILES)/tool.make

Now, you are ready to compile your first Objective-C program on Windows:

1 make CC=clang

In the above I’ve specifically instructed make to use Clang as the default compiler.

You can run your executable with:

1 ./obj/test

If you want to learn more about Objective-C I recommend you two books: Programming in Objective-C by Stephen G. Kochan and Objective-C Programming by Aaron Hillegass.


Show Comments